<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午9:25
 */


class BackOrderService extends ServiceBase {


    public function getList($inputs = array(), $pageCount = null, $isPaginate = true){
        //允许排序的字段名称数组
        $canOrderCols = array('id', 'status', 'send_meal_time', 'get_meal_time', 'updated_at', 'created_at');

        $order = $this->getSearchField($inputs, 'order');
        $reverse = $this->getSearchField($inputs, 'reverse', 0 );
        $page = $this->getSearchField($inputs, 'page', 1 );

        $member_id = $this->getSearchField($inputs,  'member_id', null );
        $status = $this->getSearchField($inputs,  'status', null );
        $payment_type = $this->getSearchField($inputs,'payment_type',null);
        $total_price_start = $this->getSearchField($inputs,  'total_price_start', null );
        $total_price_end = $this->getSearchField($inputs,  'total_price_end', null );
        $send_meal_time_start = $this->getSearchField($inputs,  'send_meal_time_start', null );
        $send_meal_time_end = $this->getSearchField($inputs,  'send_meal_time_end', null );
        $get_meal_time_start = $this->getSearchField($inputs,  'get_meal_time_start', null );
        $get_meal_time_end = $this->getSearchField($inputs,  'get_meal_time_end', null );
        $created_at_start = $this->getSearchField($inputs,  'created_at_start', null );
        $created_at_end = $this->getSearchField($inputs,  'created_at_end', null );
        $updated_at_start = $this->getSearchField($inputs,  'updated_at_start', null );
        $updated_at_end = $this->getSearchField($inputs,  'updated_at_end', null );

        $entries = Order::orderby(in_array($order, $canOrderCols)?$order:'updated_at', $reverse == 0?'desc':'asc');

        if(!empty($total_price_start)){
            $entries = $entries->where('total_price', '>=', $total_price_start);
        }

        if(!empty($total_price_end)){
            $entries = $entries->where('total_price', '<=', $total_price_end);
        }

        if(!empty($send_meal_time_start)){
            $send_meal_time_start = strtotime($send_meal_time_start);
            $send_meal_time_start = date('Y-m-d', $send_meal_time_start).' 00:00:00';

            $entries = $entries->where('send_meal_time', '>=', $send_meal_time_start);
        }

        if(!empty($send_meal_time_end)){
            $send_meal_time_end = strtotime($send_meal_time_end);
            $send_meal_time_end = date('Y-m-d', $send_meal_time_end).' 23:59:59';

            $entries = $entries->where('send_meal_time', '<=', $send_meal_time_end);
        }

        if(!empty($get_meal_time_start)){
            $get_meal_time_start = strtotime($get_meal_time_start);
            $get_meal_time_start = date('Y-m-d', $get_meal_time_start).' 00:00:00';

            $entries = $entries->where('get_meal_time', '>=', $get_meal_time_start);
        }

        if(!empty($get_meal_time_end)){
            $get_meal_time_end = strtotime($get_meal_time_end);
            $get_meal_time_end = date('Y-m-d', $get_meal_time_end).' 23:59:59';

            $entries = $entries->where('get_meal_time', '<=', $get_meal_time_end);
        }

        if(!empty($created_at_start)){
            $created_at_start = strtotime($created_at_start);
            $created_at_start = date('Y-m-d', $created_at_start).' 00:00:00';
//var_dump($created_at_start);die;
            $entries = $entries->where('created_at', '>=', $created_at_start);
        }

        if(!empty($created_at_end)){
            $created_at_end = strtotime($created_at_end);
            $created_at_end = date('Y-m-d', $created_at_end).' 23:59:59';

            $entries = $entries->where('created_at', '<=', $created_at_end);//echo $created_at_end;die;
        }

        if(!empty($updated_at_start)){
            $updated_at_start = strtotime($updated_at_start);
            $updated_at_start = date('Y-m-d', $updated_at_start).' 00:00:00';

            $entries = $entries->where('updated_at', '>=', $updated_at_start);
        }

        if(!empty($updated_at_end)){
            $updated_at_end = strtotime($updated_at_end);
            $updated_at_end = date('Y-m-d', $updated_at_end).' 23:59:59';

            $entries = $entries->where('updated_at', '<=', $updated_at_end);
        }

        if(!empty($member_id)){
            $entries = $entries->where('member_id', $member_id);
        }

        if(!empty($status)){
            $entries = is_array($status) ? $entries->whereIn('status', $status) : $entries->where('status', $status);
        }

        if(!empty($payment_type)){
            $entries = is_array($payment_type) ? $entries->whereIn('payment_type',$payment_type) : $entries->where('payment_type',$payment_type);
        }

        $pageCount = is_numeric($pageCount) ? $pageCount : Config::get('app.pagenate_num');

        $entriesTemp = clone $entries;
        $entriesTemp->select('total_price');
        $sum = $entriesTemp->sum('total_price');

        if(!$isPaginate){

            return $entries->get();
        }else{

            $entries = $entries->paginate($pageCount)
                ->addQuery('order',$order)
                ->addQuery('reverse',$reverse)

                ->addQuery('total_price_start',$total_price_start)
                ->addQuery('total_price_end',$total_price_end)
                ->addQuery('created_at_start',$created_at_start)
                ->addQuery('created_at_end',$created_at_end)
                ->addQuery('updated_at_start',$updated_at_start)
                ->addQuery('updated_at_end',$updated_at_end)
                ->addQuery('member_id',$member_id)
                ->addQuery('status',$status);

            $links = array(
                "order"=>$order,
                "reverse"=>$reverse,
                "page"=>$page,
                'total_price_start' => $total_price_start,
                'total_price_end' => $total_price_end,
                'created_at_start' => $created_at_start,
                'created_at_end' => $created_at_end,
                'updated_at_start' => $updated_at_start,
                'updated_at_end' => $updated_at_end,
                'member_id' => $member_id,
                'status' => $status,
            );

            $status_for_display = $this->prepareCheckboxDisplayForSearch(Order::$statuses,'display_name', $status);

            $payment_type_display = $this->prepareCheckboxDisplayForSearch(Order::$paymentTypes,'display_name',$payment_type);
            return array(
                'entries'=>$entries,
                'links'=>$links,
                'status_for_display'=>$status_for_display,
                'payment_type_display'=>$payment_type_display,
                'sum'=>$sum,
            );
        }

    }

    private function getSearchField($inputs, $key, $default = null){
        return isset($inputs[$key]) ? $inputs[$key] : Input::get($key, $default);
    }

    /**
     * 获取所有已出票未备菜的订单
     * @return [type] [description]
     */
    public static function getAllOrderInfo()
    {
        return Order::where('status', 2)->get();
    }

    /**
     *  显示今日订单
     */
    public static function getAllOrderTodayInfo($arrInput)
    {
        return OrderToday::getList($arrInput);
    }
    /**
     * 切换状态
     */
    public static function updateStatus($arrInput)
    {
        return OrderToday::updateStatus($arrInput);
    }
}

